package com.fare.fdc.action.flight;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.annotation.Resource;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fare.taluo.BaseFdcAction;
import com.fare.taluo.application.plane.cabin.ICabinGradeApplication;
import com.fare.taluo.application.plane.channel.SourceApplication;
import com.fare.taluo.application.plane.city.PortApplication;
import com.fare.taluo.application.plane.flight.FlightInfoApplication;
import com.fare.taluo.util.I18nUtil;
import com.fare.taluo.util.JsonUtil;
import com.fare.taluo.vo.core.FlightInfoVO;
import com.fare.taluo.vo.core.SourceEntityVO;
import com.fare.taluo.vo.core.SourceTempVO;
import com.fare.taluo.vo.plane.cabin.CabinGradeVO;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 公共Action 基类
 * 
 * @author spring.华
 */
@Controller("commonAction")
@Scope("prototype")
public class CommonAction extends BaseFdcAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1364210722878767663L;
	
	@Resource(name = "flightInfoApplication")
	private FlightInfoApplication flightInfoApplication;
	@Resource(name = "sourceApplication")
	private SourceApplication sourceApplication;
	@Resource(name = "portApplication")
	private PortApplication portApplication;

	@Resource(name = "cabinGradeAppliction")
	private ICabinGradeApplication cabinGradeApplication;

	public static List<SourceEntityVO> sourceList = null;
	private String fromCity;
	private String toCity;
	private String fromCityCode;
	private String toCityCode;
	private List<SourceTempVO> sourceTempList;

	

	/**
	 * 根据航线输出航班号(航班号前有时间)
	 * 
	 * @return
	 */
	public void outCityFlightNo() {
		//String depCity = request.getParameter("depCity");
		///String arrCity = request.getParameter("arrCity");
		String carrier = request.getParameter("carrier");
		String fromCityCodes=null,toCityCodes=null;
		if(fromCityCode ==null || fromCityCode.equals("")){
			fromCityCodes=request.getParameter("fromCityCode");
		}else{
			fromCityCodes=fromCityCode;
		}
		
		if(toCityCode==null || toCityCode.equals("")){
			toCityCodes=request.getParameter("toCityCode");
		}else{
			toCityCodes=toCityCode;
		}
		//fromCity = request.getParameter("fromCity");
		//toCity = request.getParameter("fromCity");
		if (sourceList == null) {
			sourceList = sourceApplication.findAllSourceEntity();
		}
		String carrier_key = "";
		Long channelId = 0l;
		for (Iterator iterator = sourceList.iterator(); iterator.hasNext();) {
			SourceEntityVO svo = (SourceEntityVO) iterator.next();
			if (svo.getHbaseKey().equals(carrier)) {
				carrier_key = svo.getCarrierKey();
				channelId = svo.getId();
				break;
			}
		}
//		String toCode = StringUtils.isNotEmpty(fromCity) ? portApplication
//				.findPortEntityByCityName(fromCity).getPortCode() : null;
//		String arrCode = StringUtils.isNotEmpty(this.toCity) ? portApplication
//				.findPortEntityByCityName(this.toCity).getPortCode() : null;
				
//				 List<PortEntity> formCitys = StringUtils.isNotEmpty(fromCity) ? portApplication
//						.findPortEntityByCityNames(fromCity) : null;
//				List<PortEntity> toCitys = StringUtils.isNotEmpty(this.toCity) ? portApplication
//						.findPortEntityByCityNames(this.toCity) : null;
//		List<FlightInfoVO> flights = flightInfoApplication.findByLine(formCitys,
//				toCitys, carrier_key);
		List<FlightInfoVO> flights =Lists.newArrayList();
		List<FlightInfoVO> flightInfo =Lists.newArrayList();
		flightInfo= this.flightInfoApplication.findByLine(fromCityCodes, toCityCodes, carrier_key);
//		flights = generateOnly(flights);
		//过滤掉相同的航班号
		if(flightInfo.size()>0){
			flights=flightNoFilter(flightInfo);
		}
		
		try {
			// String data=JsonUtil.toJson(flights);
			StringBuffer sb = new StringBuffer();
			for (Iterator iterator = flights.iterator(); iterator.hasNext();) {
				FlightInfoVO flightInfoVO = (FlightInfoVO) iterator.next();
				if (iterator.hasNext())
					sb.append(flightInfoVO.getFlightStartTime() + "="
							+ flightInfoVO.getFlightNo() + "#");
				else
					sb.append(flightInfoVO.getFlightStartTime() + "="
							+ flightInfoVO.getFlightNo());

			}
			sb.append("#channelId="+channelId);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.write(sb.toString());
			out.close();
		} catch (Exception e) {
		}
	}

	/**
	 * 过滤掉相同的航班号
	*/
	public List<FlightInfoVO> flightNoFilter(List<FlightInfoVO> flights){
		Map<String,FlightInfoVO> map=Maps.newHashMap();
		List<FlightInfoVO>flightLists=Lists.newArrayList();
		if(flights.size()>0){
			for(FlightInfoVO vo:flights){
				map.put(vo.getFlightNo(), vo);
			}
		}
		if(map.size()>0){
			for(Map.Entry<String, FlightInfoVO> maps:map.entrySet()){
				flightLists.add(maps.getValue());
			}
		}
		if(flightLists.size()>0)
			return flightLists;
		return null;
	}
	
	
	
	/**
	 * 根据航线输出航班号(航班号前无时间)
	 */
	public void outCityFlightNo2() {
		String depCity = request.getParameter("depCity");
		String arrCity = request.getParameter("arrCity");
		String carrier = request.getParameter("carrier");
		if (sourceList == null) {
			sourceList = sourceApplication.findAllSourceEntity();
		}
		String carrier_key = "";
		for (Iterator iterator = sourceList.iterator(); iterator.hasNext();) {
			SourceEntityVO svo = (SourceEntityVO) iterator.next();
			if (svo.getHbaseKey().equals(carrier)) {
				carrier_key = svo.getCarrierKey();
				break;
			}
		}
		String toCode = portApplication.findPortEntityByCityName(depCity)
				.getPortCode();
		String arrCode = portApplication.findPortEntityByCityName(arrCity)
				.getPortCode();
		List<FlightInfoVO> flights =Lists.newArrayList();
		List<FlightInfoVO> flightInfo =Lists.newArrayList();
		flightInfo = flightInfoApplication.findByLine(toCode,
				arrCode, carrier_key);
		//过滤掉相同的航班号
		if(flightInfo.size()>0){
			flights=flightNoFilter(flightInfo);
		}
		try {
			// String data=JsonUtil.toJson(flights);
			StringBuffer sb = new StringBuffer();
			for (Iterator iterator = flights.iterator(); iterator.hasNext();) {
				FlightInfoVO flightInfoVO = (FlightInfoVO) iterator.next();
				if (iterator.hasNext())
					sb.append(flightInfoVO.getFlightNo() + "#");
				else
					sb.append(flightInfoVO.getFlightNo());
			}
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.write(sb.toString());
			out.close();
		} catch (Exception e) {
		}
	}

	/**
	 * 根据出发地及目的地找出抓取配置中配置了该航线的航空公司
	 * 
	 * @throws JsonGenerationException
	 * @throws JsonMappingException
	 * @throws IOException
	 * @author 416428406@qq.com
	 */
	public void findExistCompanyByCrawlLine() throws Exception {
		sourceTempList = new ArrayList<SourceTempVO>();
		//PortEntityVO fromVO = portApplication
		//		.findPortEntityByCityName(fromCity);
		//PortEntityVO toVO = portApplication.findPortEntityByCityName(toCity);
		//sourceApplication.findSourceEntityByName(toCity);
		// 根据出发地和目的地查询存在航线的公司，使用group by
		List<SourceEntityVO> tempList = sourceApplication
				.findSourceEntityListByCrawlLine(fromCityCode,
						toCityCode);
		Locale locale=super.getLocale();
		I18nUtil.sourceConvert2I18N(tempList, locale);
		// 根据carrierKey找出公司信息
		/*for (Iterator iterator = tempList.iterator(); iterator.hasNext();) {
			SourceEntityVO sourceEntityVO = (SourceEntityVO) iterator.next();
			// 过滤所有渠道
			if (!sourceEntityVO.getCarrierKey().equals("XC")
					&& !sourceEntityVO.getCarrierKey().equals("YL")
					&& !sourceEntityVO.getCarrierKey().equals("TB")
					&& !sourceEntityVO.getCarrierKey().equals("QN")) {
				sourceTempList.add(sourceApplication
						.findSourceEntityListByCarrierKey(
								sourceEntityVO.getCarrierKey()).get(0));
			}

		}*/
		String jsonString = JsonUtil.toJson(getReturnList(tempList));
		super.toJSONOut(jsonString);
	}
	private List<SourceTempVO> getReturnList(List<SourceEntityVO> tempList){
		List<SourceTempVO> returnList = new ArrayList<SourceTempVO>();
		for (SourceEntityVO sourceEntityVO : tempList) {
			SourceTempVO vo = new SourceTempVO();
			vo.setCarrierKey(sourceEntityVO.getCarrierKey());
			vo.setCompanyName(sourceEntityVO.getName());
			vo.setHbaseKey(sourceEntityVO.getHbaseKey());
			vo.setIdAndHbaseKeyAndName(sourceEntityVO.getIdAndHbaseKeyAndName());
			vo.setShortName(sourceEntityVO.getShortName());
			returnList.add(vo);
		}
		
		
		return returnList;
	}
	public void findBychannelId() throws Exception {
		Long channelId=Long.parseLong(request.getParameter("channelId"));
		List<CabinGradeVO> cabinGradeVOs = cabinGradeApplication
				.list(channelId);
			String jsonString = JsonUtil.toJson(cabinGradeVOs);
			super.toJSONOut(jsonString);
	}

	public static List<SourceEntityVO> getSourceList() {
		return sourceList;
	}

	public static void setSourceList(List<SourceEntityVO> sourceList) {
		CommonAction.sourceList = sourceList;
	}

	public String getFromCity() {
		return fromCity;
	}

	public void setFromCity(String fromCity) {
		this.fromCity = fromCity;
	}

	public String getToCity() {
		return toCity;
	}

	public void setToCity(String toCity) {
		this.toCity = toCity;
	}

	public List<SourceTempVO> getSourceTempList() {
		return sourceTempList;
	}

	public void setSourceTempList(List<SourceTempVO> sourceTempList) {
		this.sourceTempList = sourceTempList;
	}

	public String getFromCityCode() {
		return fromCityCode;
	}

	public void setFromCityCode(String fromCityCode) {
		this.fromCityCode = fromCityCode;
	}

	public String getToCityCode() {
		return toCityCode;
	}

	public void setToCityCode(String toCityCode) {
		this.toCityCode = toCityCode;
	}
	
}
